<?php
$SITEROOT = '../';
include_once($SITEROOT . 'assets/snippets/shoppingCart/dbconfig.inc.php');
include_once($SITEROOT . 'assets/snippets/shoppingCart/kiwee.class.php');

$kiwee = new Kiwee();

if(IN_MANAGER_MODE!="true") die('<b>' . $_lang['kiwee_include_order_error'] . '</b>');
if ($_SESSION['mgrPermissions']['settings'] != "1") { echo 'Insufficient permissions for this module.'; exit; }

$theme = $manager_theme ? "$manager_theme/":"";
$modx->manager->initPageViewState();

function getDefaultOptions($name, $type, $instance) {
	if ($type=='name'){
		$optstoget = $instance->getListOptionsByName($name);
	} else if ($type=='id'){
		$optstoget = $instance->getListOptions($name);
	}
	
	for ($i=0; $i < sizeof($optstoget); $i++){
		$optdiv .= "  \t" . '<option value="' . $optstoget[$i]['id'] . '" >' . $optstoget[$i]['name']. '</option><br />' . "\n";
	}
	return $optdiv;
}
?>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/modal_window/javascripts/modal.js"></script>
<script type="text/javascript" src="actions/includes/interface.js"></script>
<script type="text/javascript" src="actions/includes/dynform.js"></script>
<link href="js/modal_window/stylesheets/modal.css" rel="stylesheet" type="text/css" />
<link href="actions/includes/kiwee.css" rel="stylesheet" type="text/css" />

<script>	
	function options_set_list() {
		var poststr = "trxntype=options_listentries&list=" + urival('optlist');
		makePOSTRequest('actions/ajax.php', poststr, 'options_listentries');
	}
	
	function uoptions_set_list() {
		var poststr = "trxntype=options_listentries&list=" + urival('uoptlist');
		makePOSTRequest('actions/ajax.php', poststr, 'uoptions_listentries');
	}
	
    function options_gridload(){
		makePOSTRequest('actions/ajax.php', "trxntype=options_gridload", 'options_gridload');
	}
	
	function options_all_off() {
		document.getElementById('optset_size').hide();
		document.getElementById('optset_maxlen').hide();
		document.getElementById('optset_rows').hide();
		document.getElementById('optset_cols').hide();
		document.getElementById('optset_list').hide();
		document.getElementById('optset_default').hide();
	}
	
	function options_set_render() {
		options_all_off();
		switch(document.getElementById('optrender').value) {
			case "TEXT":
				document.getElementById('optset_size').show();
				document.getElementById('optset_maxlen').show();
				break;
	
			case "TEXTAREA":
				document.getElementById('optset_rows').show();
				document.getElementById('optset_cols').show();
				break;
					
			case "DROP":
			case "CHECK":
			case "RADIO":
				document.getElementById('optset_list').show();
				document.getElementById('optset_default').show();
				break;
	
			case "MULTI":
				document.getElementById('optset_list').show();
				document.getElementById('optset_default').show();
				document.getElementById('optset_size').show();
				break;
		}	
	}
	
	function uoptions_all_off() {
		document.getElementById('uoptset_size').hide();
		document.getElementById('uoptset_maxlen').hide();
		document.getElementById('uoptset_rows').hide();
		document.getElementById('uoptset_cols').hide();
		document.getElementById('uoptset_list').hide();
		document.getElementById('uoptset_default').hide();
	}
	
	function uoptions_set_render() {
		uoptions_all_off();
		switch(document.getElementById('uoptrender').value) {
			case "TEXT":
				document.getElementById('uoptset_size').show();
				document.getElementById('uoptset_maxlen').show();
				break;
	
			case "TEXTAREA":
				document.getElementById('uoptset_rows').show();
				document.getElementById('uoptset_cols').show();
				break;
					
			case "DROP":
			//added by Bright @20080806 
			case "SELECTANDEDIT":
			case "CHECK":
			case "RADIO":
				document.getElementById('uoptset_list').show();
				document.getElementById('uoptset_default').show();
				break;
	
			case "MULTI":
				document.getElementById('uoptset_list').show();
				document.getElementById('uoptset_default').show();
				document.getElementById('uoptset_size').show();
				break;
		}	
	}
	
	function options_add() {
		var poststr = "name=" + urival('optname') + "&list=" + urival('optlist') + "&renderas=" + urival('optrender') +
					  "&htmlclass=" + urival('optclass') + "&size=" + urival('optsize') +
					  "&maxlen=" + urival('optmaxlen') + "&rows=" + urival('optrows') + "&cols=" + urival('optcols') +
					  "&default=" + urival('optdefault') + "&trxntype=options_add";
		makePOSTRequest('actions/ajax.php', poststr, 'options_gridload');
	}
	
	function uoptions_update(id) {
		var poststr = "id=" + id + "&name=" + urival('uoptname') + "&list=" + urival('uoptlist') + "&renderas=" + urival('uoptrender') +
					  "&htmlclass=" + urival('uoptclass') + "&size=" + urival('uoptsize') +
					  "&maxlen=" + urival('uoptmaxlen') + "&rows=" + urival('uoptrows') + "&cols=" + urival('uoptcols') +
					  "&default=" + urival('uoptdefault') + "&trxntype=options_update";
		makePOSTRequest('actions/ajax.php', poststr, 'options_gridload');
		hideBox();
	}
	
	
	
	
	
	function options_delete(id) {
		if (confirm("Are you sure you want to delete this option?") == true) {
			makePOSTRequest('actions/ajax.php', "trxntype=options_delete&id=" + id, 'options_gridload');
		}
	}
	
	function options_properties(id) {
		makePOSTRequest('actions/ajax.php', "trxntype=options_properties&id=" + id, 'options_properties');
		showBox();
	}
	
	function ajaxrendercomponent(name) {
		if (http_request.responseText.substring(0, 5) == "ERROR") {
			alert(http_request.responseText);
		} else if (name == 'options_listentries') {
			document.getElementById('optdefault').innerHTML = http_request.responseText;
		} else if (name == 'uoptions_listentries') {
			document.getElementById('uoptdefault').innerHTML = http_request.responseText;
		} else if (name == 'options_gridload'){ 
			document.getElementById('options_grid').innerHTML = http_request.responseText; 
        } else if (name == 'options_properties') {
			document.getElementById('inner-box').innerHTML=http_request.responseText;
		} else {
			alert(name);
		}
	}
	options_gridload();
</script>

<div id="overlay" onclick="hideBox()" style="display:none"></div>
<div id="box" style="display:none">
	<img id="close" src="js/modal_window/images/close.gif" onclick="hideBox()" alt="Close" title="Close this Window" />
	<div id="title" style="color: #ff9933; font-weight: bold; font-size: 14px;">Option Properties</div>
	<br />
	<div id="inner-box"></div>
</div>

<div class="kiwee-msc" id="mascot"><a href="http://www.kiweecommerce.org"><img src="actions/includes/Kiwee-small.gif" /></a></div>		
<div class="subTitle"></div>

<div class="sectionHeader"><?php echo $_lang["kiwee_options_header"]; ?></div>
<div class="sectionBody">
	<p><?php echo $_lang['kiwee_options_desc']; ?></p>
<div><br />
	
<!--<form method="POST" action="">  Edited by Bright @ 20080805-->
<table border=0>
<tr id="optset_name"><td><label><?php echo $_lang['kiwee_options_fld_name'] ?>:</label></td><td width="20">&nbsp;</td><td><input id="optname" type="text" length=20 /></td></tr>
<tr id="optset_render"><td><label><?php echo $_lang['kiwee_options_fld_renderas'] ?>:</label></td><td width="20">&nbsp;</td>	<td><select onchange="options_set_render();" id="optrender">
	<option value="DROP" >Drop Down</option>
	<option value="TEXT" >Text Box</option>
	<option value="TEXTAREA" >Text Area</option>
	<option value="CHECK" >Check Boxes</option>
	<option value="MULTI" >Multi Select</option>
	<option value="RADIO" >Radio Buttons</option>
	<!--added by Bight @ 20080806-->
	<option value="SELECTANDEDIT" >Select and Edit</option>	
</select><br />
<tr id="optset_list"><td><label><?php echo $_lang['kiwee_options_fld_list'] ?>:</label></td><td width="20">&nbsp;</td>	<td><select id="optlist" onchange="options_set_list();">
<?php
	$opts = $kiwee->getAllLists();
	for ($i=0; $i < sizeof($opts); $i++){
		echo '<option value="' . $opts[$i]['id'] . '" />' . $opts[$i]['description'] . '</option>';
	}
?>
</select></td></tr>
<tr id="optset_default"><td><label><?php echo $_lang['kiwee_options_fld_default'] ?>:</label></td><td width="20">&nbsp;</td><td><select id="optdefault"/>
	<?php echo getDefaultOptions($opts[0]['id'], 'id', $kiwee); ?>
</select></td></tr>
<tr id="optset_size" style="display:none"><td><label><?php echo $_lang['kiwee_options_fld_size'] ?>:</label></td><td width="20">&nbsp;</td><td><input id="optsize" type="text" length=10 /></td></tr>
<tr id="optset_maxlen" style="display:none"><td><label><?php echo $_lang['kiwee_options_fld_maxlen'] ?>:</label></td><td width="20">&nbsp;</td><td><input id="optmaxlen" type="text" length=10 /></td></tr>
<tr id="optset_rows" style="display:none"><td><label><?php echo $_lang['kiwee_options_fld_rows'] ?>:</label></td><td width="20">&nbsp;</td><td><input id="optrows" type="text" length=10 /></td></tr>
<tr id="optset_cols" style="display:none"><td><label><?php echo $_lang['kiwee_options_fld_cols'] ?>:</label></td><td width="20">&nbsp;</td><td><input id="optcols" type="text" length=10 /></td></tr>
<tr id="optset_class"><td><label><?php echo $_lang['kiwee_options_fld_htmlclass'] ?>:</label></td><td width="20">&nbsp;</td><td><input id="optclass" type="text" length=100 /></td></tr>
</table>
<button onClick="options_add()"><?php echo $_lang['kiwee_options_add'] ?></button>
</form>
<div id="options_grid" name="options_grid" ></div>
</div>
